<template>
  <a-date-picker
    v-bind="$attrs"
    v-on="$attrs"
    :showTime="showTime"
    :format="timeOptions.format"
    :valueFormat="timeOptions.valueFormat"
    :placeholder="placeholder"
    allow-clear
    :time-picker-props="timePickerOptions"
    :popupContainer="popupContainer"
    :trigger-props="{ updateAtScroll: true }"
  />
</template>

<script setup>
  import { computed } from 'vue'

  const props = defineProps({
    placeholder: {
      type: String,
      default: '请选择',
    },
    popupContainer: {
      type: [String, Object],
      default: 'body',
    },
    showTime: Boolean,
    format: String,
    valueFormat: String,
    timePickerProps: Object,
  })

  // 时间配置
  const timeOptions = computed(() => {
    if (props.showTime) {
      return {
        format: 'YYYY-MM-DD HH:mm',
        valueFormat: 'YYYY-MM-DD HH:mm:00',
      }
    } else {
      return {
        format: props.format || 'YYYY-MM-DD',
        valueFormat: props.valueFormat || 'YYYY-MM-DD',
      }
    }
  })

  // 显示时间配置
  const timePickerOptions = computed(() => {
    if (props.timePickerProps) return props.timePickerProps
    return props.showTime ? { format: 'HH:mm', defaultValue: '00:00' } : ''
  })
</script>
